package net.cruciblesoftware.ghostspacekillah;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import java.util.Random;

abstract class FieldActor {
    public static final int TYPE_PLAYER = Types.PLAYER;
    public static final int TYPE_ENEMY = Types.ENEMY;
    public static final int TYPE_BULLET = Types.BULLET;
    public static final int TYPE_EXPLOSION = Types.EXPLOSION;

    protected static Random rand;

    protected Bitmap img;
    protected int imgWidth, imgHeight;
    protected Rect src, dest;
    protected int nFrames, currentFrame;
    protected float frameStepTime;
    protected float frameTime;

    // position is center of image in real pixels, rotation in degrees
    protected float posX, posY;
    protected int rotation;

    protected int type;
    protected int state;

    public FieldActor(int t) {
        type = t;
        if(rand == null)
            rand = new Random();
        src = new Rect();
        dest = new Rect();
    }

    protected void setImage(Bitmap bmp) {
        img = bmp;
        imgHeight = img.getHeight();
        imgWidth = imgHeight;
        nFrames = img.getWidth() / imgHeight;
        currentFrame = 0;
        frameTime = 0.0f;
        src.top = 0;
        src.left = 0;
        src.right = imgWidth;
        src.bottom = imgHeight;
    }


    protected boolean detectCollision(FieldActor fa) {
        // test passed FieldActors
        if(fa.type == FieldActor.TYPE_BULLET && fa.state == Bullet.STATE_INACTIVE)
            return false;
        if((fa.state == Ship.STATE_EXPLODING || fa.state == Ship.STATE_DEAD) &&
           (fa.type == FieldActor.TYPE_ENEMY || fa.type == FieldActor.TYPE_PLAYER))
            return false;
        // test this FieldActor
        if(type == FieldActor.TYPE_BULLET && state == Bullet.STATE_INACTIVE)
            return false;
        if((state == Ship.STATE_EXPLODING || state == Ship.STATE_DEAD) &&
           (type == FieldActor.TYPE_ENEMY || type == FieldActor.TYPE_PLAYER))
            return false;

        float faRad = fa.imgWidth / 2.0f;
        float rad = imgWidth / 2.0f;
        float minDistSqr = ((faRad+rad)*(faRad+rad));
        float distSqr = ((fa.posX - posX) * (fa.posX - posX)) +
            ((fa.posY - posY) * (fa.posY - posY));
        if(distSqr < minDistSqr)
            return true;
        return false;
    }


    public int getState() {
        return state;
    }


    public abstract void update(float elapsed);
    public abstract void draw(Canvas c);
}
